import { useConfigureStore } from '@/store/configure'
import axios, { AxiosError, type AxiosRequestHeaders, type AxiosResponse } from 'axios'
import { storeToRefs } from 'pinia'

/* 定义response对象的data接口 */
interface ResponseData<T> {
  code: number
  data: T
  message: string
}

// 配置新建一个 axios 实例
const fileRequest = axios.create({
  baseURL: '/kl',
  timeout: 5000000
})

// const useLoading = useConfigureStore()

// 添加请求拦截器
fileRequest.interceptors.request.use((config) => {
  // useLoading.startLoading()
  const token = sessionStorage.getItem('Authorization')
  if (token) {
    ;(config.headers as AxiosRequestHeaders).Authorization = token
  } else {
    ;(config.headers as AxiosRequestHeaders).Authorization = `309_token`
  }
  config.responseType = 'blob'
  return config
})

// 添加响应拦截器
fileRequest.interceptors.response.use(
  /* 约束一下response */
  (response: AxiosResponse<ResponseData<any>>) => {
    // useLoading.stopLoading()
    return response
    // let res = response.data;
    // if (res.code === 200) {
    // 	return res.data
    // } else {
    // 	return Promise.reject(response.data.message)
    // }
  }
)

export default fileRequest
